package lambda_expression.jdk;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;

/**
 * Consumer
 * 接我一个参数T，不返回结果
 */
public class ConsumerTest {
    public static void main(String[] args) {
        Consumer<String> consumer = (String name) -> {
            if(name.equals("admin")) {
                System.out.println("管理员");
            } else {
                System.out.println("普通用户");
            }
        };

        consumer.accept("admin");

        System.out.println("===============================================");

        happyTime(500, s -> System.out.print(s));

        System.out.println("===============================================");

        // 字符串拼接
        System.out.println(append("hello", "world", (a, b) -> a + b));
        System.out.println(append().apply("你好", "世界"));
    }

    /**
     * 两个字符串的拼接
     */
    public static String append(String a, String b, BiFunction<String, String, String> appendMapper) {
        return appendMapper.apply(a, b);
    }

    public static BiFunction<String, String, String> append() {
        StringBuffer sb = new StringBuffer();
        return (s1, s2) -> {
            sb.append(s1);
            sb.append(s2);
            return sb.toString();
        };
    }


    public static void happyTime(Integer money, Consumer consumer) {
        consumer.accept(money);
    }
}
